package com.shengbo.product.controller;

import com.shengbo.product.service.ProductService;
import com.shengbo.product.vo.AttrVo;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/product/")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Resource
    RabbitTemplate rabbitTemplate;

    @GetMapping("/select/{id}")
    public AttrVo getAttrVo(@PathVariable("id") int attrId){
        AttrVo attrVo = productService.getAttrVo(attrId);
        return attrVo;
    }

    @GetMapping("/insert/{id}/{name}")
    public int insertAttrVo(@PathVariable("id") int attrId, @PathVariable("name") String attrName){
        AttrVo attrVo = new AttrVo();
        attrVo.setAttrId(attrId);
        attrVo.setAttrName(attrName);
        int insertResult = productService.insertAttrVo(attrVo);
        return insertResult;
    }

    @GetMapping("/update/{id}/{name}")
    public int updateAttrVo(@PathVariable("id") int attrId, @PathVariable("name") String attrName){
        AttrVo attrVo = new AttrVo();
        attrVo.setAttrId(attrId);
        attrVo.setAttrName(attrName);
        int updateResult = productService.updateAttrVo(attrVo);
        return updateResult;
    }

    @GetMapping("/delete/{id}")
    public int deleteAttrVo(@PathVariable("id") int attrId){
        int deleteResult = productService.deleteAttrVo(attrId);
        return deleteResult;
    }

    @GetMapping("/sendMessage/{id}")
    public String sendMessage(@PathVariable("id") int attrId){
        AttrVo attrVo = productService.getAttrVo(attrId);
        rabbitTemplate.convertAndSend("product-attr-exchange", "topic.1.ok", attrVo);
        return "ok";
    }

    @GetMapping("/sendMessage2/{id}")
    public String sendMessage2(@PathVariable("id") int attrId){
        AttrVo attrVo = productService.getAttrVo(attrId);
        rabbitTemplate.convertAndSend("product-attr-exchange", "topic.2.ok", attrVo);
        return "ok2";
    }


}
